﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;
using DBSourceToolsLib.Composite;
using DBSourceToolsLib;
//using HCCodeParser.Suggestions;
using DBSourceToolsLib.Suggestions;

namespace TestDBSourceToolsLibNUnit
{
    [TestFixture]
    public class TestDBComposite
    {
        DBComposite m_DBComposite;

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            string currentDirectory = Directory.GetCurrentDirectory();

            string strProjectFile = Constants.AdventureWorks_ProjectFile;
            DBSourceToolsLib.DatabaseProject oDBProject = new DatabaseProject(strProjectFile);

            oDBProject.LoadFromDisk(false);
            Assert.AreEqual("AdventureWorks", oDBProject.ProjectName);
            Assert.Greater(oDBProject.SourceDatabases.Count, 0);
            Assert.AreEqual((string)"AdventureWorksLT", oDBProject.SourceDatabases[0].DatabaseName);
            Assert.AreEqual((string)"AdventureWorksLT", oDBProject.SourceDatabases[0].NickName);

            SourceDatabase oSource = oDBProject.SourceDatabases[0];

            System.Windows.Forms.Form oForm = new System.Windows.Forms.Form();

            DBSourceToolsLib.SourceDatabaseLoader oSourceLoader =
                new DBSourceToolsLib.SourceDatabaseLoader(oForm,
                    DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadFromDisk);

            oSourceLoader.Execute(ref oSource, null);

            m_DBComposite = oSource.GetDBComposite();

        }

        [Test]
        public void TestCompositeObjects()
        {
            DBComposite oDatabase = new DBComposite("Database", "ShortName",
                DBCompositeType.Database,
                new TreeNodeTag("Test", "Database", null, null));
            DBComposite oTables = new DBComposite("Tables", "ShortName",
                DBCompositeType.Tables,
                new TreeNodeTag("Test", "Database", null, null));
            DBComposite oTable1 = new DBComposite("FirstTable", "ShortName",
                DBCompositeType.Table, new TreeNodeTag("Test", "Database", null, null));
            oTable1.Add(new DBComposite("FirstField", "ShortName",
                DBCompositeType.Field, new TreeNodeTag("Test", "Database", null, null)));
            oTable1.Add(new DBComposite("SecondField", "ShortName",
                DBCompositeType.Field,  new TreeNodeTag("Test", "Database", null, null)));
            oTable1.Add(new DBComposite("ThirdField", "ShortName",
                DBCompositeType.Field, new TreeNodeTag("Test", "Database", null, null)));



            DBComposite oTable2 = new DBComposite("SecondTable", "ShortName",
                DBCompositeType.Table,  new TreeNodeTag("Test", "Database", null, null));
            oTable2.Add(new DBComposite("FirstField_2", "ShortName",
                DBCompositeType.Field,  new TreeNodeTag("Test", "Database", null, null)));
            oTable2.Add(new DBComposite("SecondField_2", "ShortName",
                DBCompositeType.Field,  new TreeNodeTag("Test", "Database", null, null)));
            oTable2.Add(new DBComposite("ThirdField_2", "ShortName",
                DBCompositeType.Field,  new TreeNodeTag("Test", "Database", null, null)));

            oTables.Add(oTable1);
            oTables.Add(oTable2);

            oDatabase.Add(oTables);


            DBComposite oViews = new DBComposite("Views", "ShortName",
                DBCompositeType.Views,  new TreeNodeTag("Test", "Database", null, null));
            DBComposite oView1 = new DBComposite("View_1", "ShortName",
                DBCompositeType.View,  new TreeNodeTag("Test", "Database", null, null));
            oView1.Add(new DBComposite("ViewField1", "ShortName",
                DBCompositeType.Field,  new TreeNodeTag("Test", "Database", null, null)));
            oView1.Add(new DBComposite("ViewField2", "ShortName",
                DBCompositeType.Field,  new TreeNodeTag("Test", "Database", null, null)));

            oViews.Add(oView1);
            oDatabase.Add(oViews);

            string strDebugString = oDatabase.GetDebugString(0);
            System.Console.WriteLine(strDebugString);

        }

        [Test]
        public void GetSysObjectsComposite()
        {
            //Constants.AdventureWorks_ProjectFile

            string strDebug = m_DBComposite.GetDebugString(0);
            Console.WriteLine(strDebug);

        }

        #region notes
        // context menus:
        // Database : m_contextMenuStripDatabase
        // Tables : m_contextMenuStripTables
        // Table : m_contextMenuStripTable
        // Views : m_contextMenuStripViews
        // View : m_contextMenuStripView
        // Procs : m_contextMenuStripProcs
        // Functions : m_contextMenuStripFunctions
        // Schemas : m_contextMenuTreeView - error
        // XmlSchema : m_contextMenuTreeView - error
        // Queries : m_contextMenuStripQueries
        // Query : m_contextMenuTreeView
        // User : m_contextMenuTreeView - error
        // Role : m_contextMenuTreeView - error
        // Table Type : m_contextMenuTreeView - error
        // Triggers : m_contextMenuTreeView - error
        // Dependancy Tree : 
        // Deployment Targets : m_contextMenuDeployTarget
        // Deployment Target : m_contextMenuStripProperties
        // Patches : m_contextMenuStripPatches
        // Patch : m_contextMenuTreeView
        #endregion

        [Test]
        public void TestFindDatabaseKeys()
        {
            List<string> lstDatabaseKeys = m_DBComposite.BuildTypeKeys(DBCompositeType.Database);
            Assert.AreEqual(1, lstDatabaseKeys.Count);
            string strKey1 = lstDatabaseKeys[0];
            Assert.AreEqual("AdventureWorksLT.Database.", strKey1);
        }

        [Test]
        public void TestFindTableKeys()
        {
            List<string> lstDatabaseKeys = m_DBComposite.BuildTypeKeys(DBCompositeType.Table);
            Assert.AreEqual(12, lstDatabaseKeys.Count);
            string strKey1 = lstDatabaseKeys[0];
            Assert.AreEqual(@"AdventureWorksLT.File.V:\windows\DBSourceTools_root_2010\TestFiles\control\AdventureWorks\AdventureWorksLT\Tables\BuildVersion.sql", strKey1);
        }

        [Test]
        public void TestFindViewKeys()
        {
            List<string> lstDatabaseKeys = m_DBComposite.BuildTypeKeys(DBCompositeType.View);
            Assert.AreEqual(3, lstDatabaseKeys.Count);
            string strKey1 = lstDatabaseKeys[0];
            Console.WriteLine(strKey1);
            Assert.AreEqual(@"AdventureWorksLT.File.V:\windows\DBSourceTools_root_2010\TestFiles\control\AdventureWorks\AdventureWorksLT\Views\SalesLT.vGetAllCategories.sql", strKey1);
        }

        [Test]
        public void TestFindViewNodeKeys()
        {
            List<string> lstDatabaseKeys = m_DBComposite.BuildTypeKeys(DBCompositeType.Views);
            Assert.AreEqual(1, lstDatabaseKeys.Count);
            string strKey1 = lstDatabaseKeys[0];
            Console.WriteLine(strKey1);
            Assert.AreEqual(@"AdventureWorksLT.Views.", strKey1);
        }

        [Test]
        public void TestFindTableList()
        {
            List<string> lstTables = SuggestionConverter.SuggestionListToSimple(
                m_DBComposite.BuildList(FieldListType.Long, DBCompositeType.Table));
            Assert.AreEqual(12, lstTables.Count);
            string strKey1 = lstTables[0];
            Assert.AreEqual(@"BuildVersion", strKey1);
        }

        [Test]
        public void TestFindViewList()
        {
            List<string> lstTables = SuggestionConverter.SuggestionListToSimple(
                m_DBComposite.BuildList(FieldListType.Long, DBCompositeType.View));
            Assert.AreEqual(3, lstTables.Count);

            StringBuilder oBuilder = new StringBuilder();
            foreach (string strFound in lstTables)
                oBuilder.AppendLine(strFound);
            Console.WriteLine(oBuilder.ToString());

            string strKey1 = lstTables[0];
            Assert.AreEqual(@"SalesLT.vGetAllCategories", strKey1);
        }

        [Test]
        public void TestFindFieldList()
        {
            List<TableAndAlias> initialTableList = new List<TableAndAlias>();
            initialTableList.Add(new TableAndAlias { TableName = "BuildVersion" });
            List<string> lstFields = SuggestionConverter.SuggestionListToSimple( 
                m_DBComposite.BuildFieldList(initialTableList ));
            Assert.AreEqual(4, lstFields.Count);

            StringBuilder oBuilder = new StringBuilder();
            foreach (string strFound in lstFields)
            {
                oBuilder.AppendLine(strFound);
            }
            Console.WriteLine(oBuilder.ToString());

            string strKey1 = lstFields[0];
            Assert.AreEqual(@"SystemInformationID", strKey1);
        }

        [Test]
        public void TestFindFieldListFromTwoTables()
        {
            List<TableAndAlias> initialTableList = new List<TableAndAlias>();
            initialTableList.Add(new TableAndAlias { TableName = "BuildVersion" });
            initialTableList.Add(new TableAndAlias { TableName = "ErrorLog" });
            List<string> lstFields = SuggestionConverter.SuggestionListToSimple( 
                m_DBComposite.BuildFieldList(initialTableList));
            Assert.AreEqual(13, lstFields.Count);

            StringBuilder oBuilder = new StringBuilder();
            foreach (string strFound in lstFields)
            {
                oBuilder.AppendLine(strFound);
            }
            Console.WriteLine(oBuilder.ToString());

            string strKey1 = lstFields[0];
            Assert.AreEqual(@"BuildVersion.SystemInformationID", strKey1);
        }

        [Test]
        public void TestFindFieldListFromTwoTablesWithAlias()
        {
            List<TableAndAlias> initialTableList = new List<TableAndAlias>();
            initialTableList.Add(new TableAndAlias { TableName = "BuildVersion", AliasName="bv" });
            initialTableList.Add(new TableAndAlias { TableName = "ErrorLog" });
            List<string> lstFields = SuggestionConverter.SuggestionListToSimple( 
                m_DBComposite.BuildFieldList(initialTableList));
            Assert.AreEqual(13, lstFields.Count);

            StringBuilder oBuilder = new StringBuilder();
            foreach (string strFound in lstFields)
            {
                oBuilder.AppendLine(strFound);
            }
            Console.WriteLine(oBuilder.ToString());

            string strKey1 = lstFields[0];
            Assert.AreEqual(@"bv.SystemInformationID", strKey1);
        }

        [Test]
        public void TestFindFieldListFromTwoTablesWithAliasSameTable()
        {
            List<TableAndAlias> initialTableList = new List<TableAndAlias>();
            initialTableList.Add(new TableAndAlias { TableName = "BuildVersion", AliasName = "bv" });
            initialTableList.Add(new TableAndAlias { TableName = "BuildVersion", AliasName = "bv2" });
            List<string> lstFields = SuggestionConverter.SuggestionListToSimple(
                m_DBComposite.BuildFieldList(initialTableList));
            //Assert.AreEqual(13, lstFields.Count);

            StringBuilder oBuilder = new StringBuilder();
            foreach (string strFound in lstFields)
            {
                oBuilder.AppendLine(strFound);
            }
            Console.WriteLine(oBuilder.ToString());

            string strKey1 = lstFields[0];
            Assert.AreEqual(@"bv.SystemInformationID", strKey1);

            Assert.True(lstFields.Contains("bv.SystemInformationID"));
            Assert.True(lstFields.Contains("bv2.SystemInformationID"));
        }


    }
}
